using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SigmaStock_Components;
using System.Data.OleDb;

namespace project
{
    public partial class Expenses : Form
    {
        OleDbConnection conn;
        string query;
        public Expenses()
        {
            InitializeComponent();
            conn=DBComponent.Mag_DealerDB.dbconnection();
            conn.Open();
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            //dbconnection();
            //insert_expense();

        }
    
        public void insert_expense() { 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (ValidateExpense())
            {
                string expense_type = comboBox1.Text;
                int amt = int.Parse(textBox3.Text);
                string pay_to = textBox2.Text;
                DateTime curr_date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
                query = "insert into expenses(CURR_DATE,EXPENSE_TYPE,AMOUNT) values ('" + curr_date + "','" + expense_type + "','" + amt + "');";
                OleDbCommand command = new OleDbCommand(query, conn);
                command.ExecuteNonQuery();

                query = "insert into transaction1(CURR_DATE,TRANSACTION_TYPE,TRANS_FROM,TRANS_TO,AMOUNT) VALUES('" + curr_date + "','" + expense_type + "','" + "KNA" + "','" + pay_to + "','" + amt + "');";
                command.CommandText = query;
                command.ExecuteNonQuery();
                MessageBox.Show("Data Inserted Successfully !!!");
            }

        }

        private bool ValidateExpense()
        {
            textBox4.Visible = true;
            if (comboBox1.SelectedItem == null)
            {
                textBox4.Text = "Select expense type";
                Console.Beep();
                return false;
            }
            if (String.IsNullOrEmpty(textBox2.Text)|| !(Validation.IsAlpha(textBox2.Text)))
            {
                textBox4.Text = "Check Expense To Field";
                Console.Beep();
                return false;
            }

            if (String.IsNullOrEmpty(textBox3.Text)||!(Validation.IsNumber(textBox3.Text)))
            {
                textBox4.Text = "Check Expense Amount Field";
                Console.Beep();
                return false;
            }
            return true;
            
        }

        private void Expenses_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (Application.OpenForms.Count == 1)
            {
                Program.main_form.Show();
            }
        }

        private void Expenses_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            comboBox1.SelectedItem = null;
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox4.Visible = false;
        
        }

     
    }
}